class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int>mp;
        for (int i = 0; i < nums.size(); i++) {
            int x = nums[i];
            if (mp.count(x) && i - mp[x] <= k)return 1;
            mp[x] = i;
        }
        return 0;
    }
};